using System;
using System.Collections.Generic;
using System.Text;
using ZWaveApi.Net;
using ZWaveApi.Net.Messenger;
using ZWaveApi.Net.CommandClasses;
using System.Threading;

namespace ControlPoint.ControlAPI {
    public class ZWaveController : DeviceController {
        private string _comPort;

        private ZWaveApi.Net.ZWaveController zc;

		private List<ZWaveApi.Net.CommandClasses.CommandClass> _sharedCommands;
		private List<ZWaveApi.Net.CommandClasses.CommandClass> _ignoredCommands;
		
        public SortedDictionary<byte, ZWaveApi.Net.ZWaveNode> Nodes {
            get {
                return zc.Nodes;
            }
        }
		
		public override Dictionary<string, string> AvailableDevices {get{
            Dictionary<string, string> ad = new Dictionary<string, string>();
            bool add = true;
				foreach (ZWaveApi.Net.ZWaveNode n in zc.Nodes.Values) {
                    foreach (ZWaveApi.Net.Utilities.CommandIndex ci in n.CommandClasses.Keys) {
                        add = true;
                        foreach (Device d in this.Devices) {
                            if (ci.NodeId  == (byte)d.Settings.GetSetting("nodeId").Value &&
                                ci.Instance == (byte)d.Settings.GetSetting("instanceId").Value &&
                                ci.Command == (ZWaveApi.Net.CommandClasses.CommandClass)d.Settings.GetSetting("command").Value) {
                                add = false;
                            }
                        }
                        if (add) {
                            ad.Add(ci.StringId(), ci.ToString());
                        }
                    }
                    //test;
				}
                return ad;
			}
        }

		public void InitCommands() {
			_sharedCommands = new List<CommandClass>();
			_sharedCommands.Add (ZWaveApi.Net.CommandClasses.CommandClass.Battery);
			_sharedCommands.Add (ZWaveApi.Net.CommandClasses.CommandClass.Configuration);
			
			
			_ignoredCommands = new List<CommandClass>();
			_ignoredCommands.Add (ZWaveApi.Net.CommandClasses.CommandClass.Association);
			_ignoredCommands.Add (ZWaveApi.Net.CommandClasses.CommandClass.WakeUp);
			_ignoredCommands.Add (ZWaveApi.Net.CommandClasses.CommandClass.MultiInstance);
			_ignoredCommands.Add (ZWaveApi.Net.CommandClasses.CommandClass.MultiInstanceAssociation);
		}
		
        public ZWaveController(SettingsList settings)
            : base() {

			InitCommands();

            this._comPort = settings.GetSetting("comPort").Value.ToString();

            zc = new ZWaveApi.Net.ZWaveController(this._comPort);
            zc.Open();


            base._id = "zw:" + this.zc.Info.homeId;
            base._name = (string)settings.GetSetting("name").Value.ToString();
        }

        public ZWaveController(string comPort)
            : base() {
			
			InitCommands();
			
            this._comPort = comPort;
            base._name = "Default ZWaveController";

            zc = new ZWaveApi.Net.ZWaveController(this._comPort);
            zc.Open();

            this._id = "zw:" + this.zc.Info.homeId;
        }

        ~ZWaveController() {
            zc.Close();
        }

        public string ComPort {
            get {
                return _comPort;
            }
            set {
                _comPort = value;
            }
        }

        public override SettingsList Settings {
            get {
                SettingsList settings = new SettingsList();
                settings.SetSetting(new Setting("comPort", this._comPort));
                settings.SetSetting(new Setting("name", base._name));
                settings.SetSetting(new Setting("type", this.ToString()));
                return settings;

            }
        }




    }
}